package com.demo.service.auth.impl;

import com.demo.common.util.bean.OrikaUtilsTool;
import com.demo.common.util.bean.TokenUtilsTool;
import com.demo.dao.auth.entity.*;
import com.demo.dao.auth.mapper.AuthUserInfoMapper;
import com.demo.service.auth.AuthUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class AuthUserInfoServiceImpl implements AuthUserInfoService {
    @Autowired
    AuthUserInfoMapper userInfoMapper;

    //获取菜单根
    @Override
    public List<AuthMenuTreeNode> buildMenuRoot(List<AuthMenuNode> menuList) {
        List<AuthMenuTreeNode> rootNode = new ArrayList<>();
        AuthMenuTreeNode node = new AuthMenuTreeNode();
        for (AuthMenuNode menu : menuList) {
            if (menu.getPid().equals(0L)){
                //System.out.println(menu);
                node = OrikaUtilsTool.<AuthMenuNode, AuthMenuTreeNode>convert(menu, AuthMenuTreeNode.class);
                rootNode.add(node);
            }
        }

        return rootNode;
    }

    //构建菜单子树
    @Override
    public AuthMenuTreeNode buildMenuChildren(List<AuthMenuNode> menuList, AuthMenuTreeNode rootNode) {
        List<AuthMenuTreeNode> childrenTree = new ArrayList<>();
        AuthMenuTreeNode node = new AuthMenuTreeNode();
        for (AuthMenuNode menu : menuList) {
            if (menu.getPid().equals(rootNode.getId())){
                node = OrikaUtilsTool.<AuthMenuNode, AuthMenuTreeNode>convert(menu, AuthMenuTreeNode.class);
                childrenTree.add(buildMenuChildren(menuList, node));
            }
        }

        rootNode.setChildren(childrenTree);
        return rootNode;
    }

    //构建菜单树
    @Override
    public List<AuthMenuTreeNode> buildMenuTree(List<AuthMenuNode> menuList) {
        List<AuthMenuTreeNode> menus = buildMenuRoot(menuList);
        //System.out.println(menus);
        for (AuthMenuTreeNode menu : menus) {
            buildMenuChildren(menuList, menu);
        }

        return menus;
    }

    //根据权限选择菜单
    @Override
    public List<AuthMenuNode> filterMenu(List<AuthMenuNode>menuList, List<AuthPermissionNode> permissionList) {
        List<AuthMenuNode> list = new ArrayList<>();
        for (AuthMenuNode menu : menuList) {
            for (AuthPermissionNode r : permissionList) {
                if (r.getFieldId().equals(menu.getId())) {
                    //System.out.println(menu);
                    list.add(menu);
                    break;
                }
            }
        }

        return list;
    }

    //根据账号和密码返回token,user对象，权限菜单
    @Override
    public AuthUserMenu getUser(AuthLoginNode login) {
        AuthUserMenu record = new AuthUserMenu();
        Map<String, String> map = new HashMap<>();
        AuthUserNode user = userInfoMapper.getUser(login);
        if (user == null) {
            System.out.print("user is null!");
            return null;
        }

        //依据权限读取权限菜单
        map.put("masterId", user.getRoleId().toString());
        map.put("fieldTypeValue", "菜单");
        List<AuthPermissionNode> permissionList = userInfoMapper.getPermissionByParams(map);
        //System.out.println(permissionList);

        //读取所有菜单
        List<AuthMenuNode> menuList = userInfoMapper.getMenu();
        //System.out.println(menuList);

        List<AuthMenuNode> filterMenuList = this.filterMenu(menuList, permissionList);
        //System.out.println(filterMenuList);

        //token
        String token = TokenUtilsTool.sign(login.getUsername());
        if (token == null) {
            System.out.print("token is null!");
            return null;
        }

        record.setToken(token);
        record.setUser(user);
        record.setMenuList(this.buildMenuTree(filterMenuList));

        return record;
    }

    //根据账号和密码返回user对象，权限菜单
    @Override
    public  AuthSearchMenu getMenu(AuthLoginNode login) {
        AuthSearchMenu record = new AuthSearchMenu();
        Map<String, String> map = new HashMap<>();
        AuthUserNode user = userInfoMapper.getUser(login);
        if (user == null) {
            System.out.print("user is null!");
            return null;
        }

        //依据权限读取权限菜单
        map.put("masterId", user.getRoleId().toString());
        map.put("fieldTypeValue", "菜单");
        List<AuthPermissionNode> permissionList = userInfoMapper.getPermissionByParams(map);
        //System.out.println(permissionList);

        //读取所有菜单
        List<AuthMenuNode> menuList = userInfoMapper.getMenu();
        //System.out.println(menuList);

        List<AuthMenuNode> filterMenuList = this.filterMenu(menuList, permissionList);
        //System.out.println(filterMenuList);

        record.setUser(user);
        record.setMenuList(this.buildMenuTree(filterMenuList));

        return record;
    }
}
